package com.hmall.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.client.ItemClient;
import com.hmall.common.client.UserClient;
import com.hmall.common.dto.Address;
import com.hmall.common.dto.Item;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.Order;
import com.hmall.order.pojo.OrderDetail;
import com.hmall.order.pojo.OrderLogistics;
import com.hmall.order.pojo.RequestParams;
import com.hmall.order.service.IOrderDetailService;
import com.hmall.order.service.IOrderLogisticsService;
import com.hmall.order.service.IOrderService;
import com.hmall.order.utils.UserHolder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.time.LocalDateTime;

@Service
public class OrderService extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Autowired
    private ItemClient itemClient;
    @Autowired
    private UserClient userClient;
    @Autowired
    private IOrderLogisticsService orderLogisticsService;
    @Autowired
    private IOrderDetailService orderDetailService;

    @Transactional
    @Override
    public Long comformOrderById(RequestParams requestParams) {
        //mybatispules自动计算雪花算法获取id
        Order order = new Order();
        //1. 商品微服务提供FeignClient，查询商品
        Item item = itemClient.ItemById(requestParams.getItemId());
        //2. 根据itemId查询商品信息
        //基于商品价格、购买数量计算商品总价：totalFee
        long totalFee = item.getPrice() * requestParams.getNum();
        order.setTotalFee(totalFee);
        order.setPaymentType(requestParams.getPaymentType());
        order.setUserId(UserHolder.getUser());
       // 封装Order对象，初识status为未支付
        order.setStatus(1);
        //3. 将Order写入数据库tb_order表中
        this.save(order);
        //4. 写入tb_order_detail表中
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setName(item.getName());
        orderDetail.setSpec(item.getSpec());
        orderDetail.setPrice(item.getPrice());
        orderDetail.setNum(requestParams.getNum());
        orderDetail.setItemId(item.getId());
        orderDetail.setImage(item.getImage());
        orderDetail.setOrderId(order.getId());
        //将商品信息、orderId信息封装为OrderDetail对象，写入tb_order_detail表
        orderDetailService.save(orderDetail);

        //5. 根据addressId查询user - service服务，获取地址信息
        Long addressId = requestParams.getAddressId();
        Address address = userClient.findAddressById(addressId);

        //6. 将地址封装为OrderLogistics对象，写入tb_order_logistics表
        OrderLogistics orderLogistics = new OrderLogistics();
        BeanUtils.copyProperties(address,orderLogistics);
        orderLogistics.setOrderId(order.getId());
        orderLogisticsService.save(orderLogistics);
        //- 11）在item - service提供减库存接口，并编写FeignClient
        //- 12）调用item - service的减库存接口
        try {
            itemClient.updateStock(requestParams.getItemId(),requestParams.getNum());
        }catch (Exception e){
            throw new RuntimeException("库存不足");
        }
        return order.getId();
}
}




